//该文件专门用于创建整个应用的路由器

import VueRouter from "vue-router";
import About from "@/pages/About";
import Home from '@/pages/Home';
import News from "@/pages/News";
import Message from "@/pages/Message";
import Detail from "@/pages/Detail";

//创建一个路由器
const router = new VueRouter({
    //默认开启hash模式
    mode: 'history',
    routes: [
        {
            name: 'regard',
            path: '/about',
            component: About,
            meta: {
                isAuth: true,
                title: '关于'
            },
        },
        {
            name: 'homepage',
            path: '/home',
            component: Home,
            meta: {
                title: '主页'
            },
            children: [
                {
                    name: 'ns',
                    path: 'news',
                    component: News,
                    //meta:路由元信息，可以配置是否需要校验的信息
                    meta: {
                        isAuth: true,
                        title: '新闻'
                    },
                    //独享路由守卫
                    // beforeEnter(to,from,next){
                    //     const { isAuth } = to.meta;
                    //     if(isAuth){
                    //          //代表需要鉴权
                    //          if(localStorage.getItem('school') === 'wust1') next();//类似于nodejs中间件
                    //          else alert('无权限');
                    //      }else{
                    //          next();
                    //      }
                    // }
                },
                {
                    name: 'msg',
                    path: 'message',
                    component: Message,
                    children: [
                        {
                            name: 'particulars',
                            path: 'detail',
                            component: Detail,
                            props ({ query: { id, title } }) {
                                return {
                                    id,
                                    title
                                }
                            }
                        }
                    ],
                    //meta:路由元信息，可以配置是否需要校验的信息
                    meta: {
                        isAuth: true,
                        title: '消息'
                    }
                }
            ]
        }
    ]
});

//全局前置路由守卫
//初始化和在每一次路由切换之前被调用
// router.beforeEach((to, from, next) => {
//     // console.log(to, from);
//     console.log('前置路由守卫');
//     const { isAuth } = to.meta;
//     if(isAuth){
//         //代表需要鉴权
//         if(localStorage.getItem('school') === 'wust1') next();//类似于nodejs中间件
//         else alert('无权限');
//     }else{
//         next();
//     }
// });

//全局后置路由守卫
//初始化和在每一次路由切换之后被调用
router.afterEach((to, from) => {
    // console.log('后置路由守卫', to, from);
    //点击每一个路由都切换西夏document.title
    const { title } = to.meta;
    document.title = title || 'vue-advance';
})


export default router;
